package com.derive.education_service.web.platform.controller;

import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Platform;
import com.derive.education_service.web.platform.bean.dto.*;
import com.derive.education_service.web.platform.service.PlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 机构应用接口
 */
@RestController
@RequestMapping("platform")
@Validated
@Slf4j
public class PlatformController {

    @Autowired
    private PlatformService platformService;

    /**
     * 先新增机构，新增成功才能上传机构资源(流程化)
     * 新增机构 -- 机构信息
     *
     * @param file
     * @param platformAdd
     * @return
     */
    @PostMapping("addPlatform")
    public ResponseMessage addPlatform(@RequestPart("file") MultipartFile file, @Validated @RequestPart("platformAdd") PlatformAdd platformAdd) {
        return platformService.addPlatform(file, platformAdd);
    }

    /**
     * 新增机构资源
     * 带机构id
     *
     * @param files
     * @param platformResAdd
     * @return
     */
    @PostMapping("addPlatformRes")
    public ResponseMessage addPlatformRes(@RequestPart("files") List<MultipartFile> files, @Validated @RequestPart("platformResAdd") PlatformResAdd platformResAdd) {
        System.out.println(files.size());
        return platformService.addPlatformRes(files, platformResAdd);
    }

    /**
     * 删除机构 -- 机构信息+机构资源信息+内容关联信息
     *
     * @param platformId
     * @return
     */
    @PostMapping("delPlatform")
    public ResponseMessage delPlatform(Integer platformId) {
        return platformService.delPlatform(platformId);
    }

    /**
     * 删除机构资源数据
     *
     * @param prId
     * @return
     */
    @PostMapping("delPlatformResByPrId")
    public ResponseMessage delPlatformResByPrId(Integer prId) {
        return platformService.delPlatformResByPrId(prId);
    }

    /**
     * 查询服务平台所有机构 -- 分页参数 -- 按条件
     *
     * @param queryParam
     * @return
     */
    @PostMapping("getPlatformList")
    public ResponseMessage getPlatformList(@RequestBody @Validated QueryParam queryParam) {
        Paper<Platform> platformPaper = platformService.getPlatformList(queryParam);
        return ResponseMessage.createBySuccess(platformPaper);
    }

    /**
     * 根据id查询机构详细信息
     *
     * @param platformId
     * @return
     */
    @PostMapping("getPlatformInfoById")
    public ResponseMessage getPlatformInfoById(Integer platformId) {
        return platformService.getPlatformInfoById(platformId);
    }

    /**
     * 编辑机构信息
     * @param file
     * @param platformUp
     * @return
     */
    @PostMapping("upPlatformById")
    public ResponseMessage upPlatformById(@RequestPart("file") MultipartFile file, @Validated @RequestPart("platformUp") PlatformUp platformUp) {
        return platformService.upPlatformById(file, platformUp);
    }
}
